package rahmath.sharif.docrypt.util;

import java.io.File;
import java.util.Comparator;

public class FileComparator implements Comparator<File> {

	private static final String DOT = ".";

	public int compare(File file1, File file2) {
		boolean isDirectory1 = file1.isDirectory();
		boolean isDirectory2 = file2.isDirectory();
		if ((isDirectory1 && isDirectory2) || (!isDirectory1 && !isDirectory2)) {
			boolean dotted1 = file1.getName().startsWith(DOT);
			boolean dotted2 = file2.getName().startsWith(DOT);
			if ((dotted1 && dotted2) || (!dotted1 && !dotted2)) {
				return file1.getName().toLowerCase()
						.compareTo(file2.getName().toLowerCase());
			}else{
				if(dotted1){
					return 1;
				}else{
					return -1;
				}
			}
		} else if (file1.isDirectory()) {
			return 1;
		} else {
			return -1;
		}
	}
}
